/**
 * Created by root on 17-7-27.
 */
var http = require("http");
var superagent = require("superagent");
var charset = require('superagent-charset');
var iconv = require("iconv-lite");
var BufferHelper = require('bufferhelper');

module.exports = {
    
    httpGetByURL : function (url, callback) {
        let content = '';
        let statusCode = 0;
        var req = http.get(url, function(res){
            res.setEncoding('utf8');
            res.on('data',function(chunk){
                content += chunk.toString();
            });
            statusCode = res.statusCode;
        });

        req.on("close", function () {
            console.log();
            callback({statusCode : statusCode, body : content});
        })

        req.on("error", function(err){
            console.log(err.message);
        });

        req.end();
    },

    data5uGet : function (url, callback) {
        charset(superagent)
        superagent('GET',url)//这里的URL也可以是绝对路径
            .set('Cache-Control', 'max-age=0')
            .set('Upgrade-Insecure-Requests', '1')
            .set('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36')
            .set('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')
            .set('Accept-Encoding', 'gzip, deflate')
            .set('Accept-Language', 'zh-CN,zh;q=0.8')
            .set('Cookie', 'auth=8719e91c79c1e98cdf4d18b7f646eff9; UM_distinctid=15d972ee23464d-09f2f921e7a057-38710357-1fa400-15d972ee235a5f; JSESSIONID=0E7686745019A10AD9388C4E8AC346AD; CNZZDATA1260383977=1022195651-1501476052-null%7C1501667636; Hm_lvt_3406180e5d656c4789c6c08b08bf68c2=1501480018,1501584730,1501660440; Hm_lpvt_3406180e5d656c4789c6c08b08bf68c2=1501671260')
            .charset('gbk')
            .end(function(req,res){
                callback({statusCode : res.statusCode, body : res.text});
            });
    },

    httpGetByHeaders : function (url, headers, callback) {
        var hostname = url.substring(0, url.indexOf('.com') + 4);
        var path = url.substring(url.indexOf('.com') + 4);
        console.log(hostname);
        console.log(path);

        const options = {
            hostname: hostname,
            path: path,
            method: 'GET',
            headers: headers,
        };

        let content = '';
        let statusCode = 0;
        let bufferHelper = new BufferHelper();
        var req = http.request(options, function(res){
            res.setEncoding('utf8');
            res.on('data',function(chunk){
                content += chunk.toString();
            });
            statusCode = res.statusCode;
        });

        req.on("close", function () {
            callback({statusCode : statusCode, body : content});
        })

        req.on("error", function(err){
            console.log(err.message);
        });

        req.end();

    },

    verifyHttp : function (ip, port, callback) {
        var opt = {
            host : ip,
            port : port,
            method : 'GET',//这里是发送的方法
            hostname : ' https://www.baidu.com',     //这里是访问的路径
            path : '/index.html'
        }

        let content = '';
        let statusCode = 0;
        var req = http.request(opt, function(res){
            res.setEncoding('utf-8');
            res.on('data',function(chunk){
                content += chunk.toString();
            });
            statusCode = res.statusCode;
        });

        req.on("close", function () {
            callback({statusCode : statusCode, body : content});
        })

        req.on("error", function(err){
            console.log(err.message);
        });
        req.end();
    }
    
}